﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using WebAppTemplate.CSR.Repository.DbContexts;

namespace WebAppTemplate.CSR.Service.Commands
{
    public record ModifyDemoCommand(long Id, string Value) : IRequest<bool>;

    public class ModifyDemoCommandHandler : IRequestHandler<ModifyDemoCommand, bool>
    {
        private readonly AppDbContext _dbContext;
        public ModifyDemoCommandHandler(AppDbContext dbContext) => _dbContext = dbContext;

        public async Task<bool> Handle(ModifyDemoCommand request, CancellationToken cancellationToken)
        {
            await _dbContext.Demo.Where(it => it.Id == request.Id)
                .ExecuteUpdateAsync(it => it.SetProperty(p => p.Value, request.Value), cancellationToken);

            return true;
        }
    }
}
